// api={parseTapmode:function(){},safeArea:{},showProgress:function(){},hideProgress:function(){}}``
apiready = function () {
  api.parseTapmode();
  FastClick.attach(document.body);
  var header = $api.byId('aui-header');
  $api.fixStatusBar(header);
  api.setRefreshHeaderInfo({
      loadingImg: 'widget://image/refresh.png',
      bgColor: '#fdd903',
      textColor: '#fff',
      textDown: '下拉刷新...',
      textUp: '松开刷新...'
  }, function(ret, err) {
    pageStatistics.getData();
  });


  $(document).on("closeMobileCalendar",function(e){
      var v = $("#calendar").val();
      if(pageStatistics.type=="month" && v!=""){
        var arr =  v.split("-");
        arr.splice(2, 1);
        $("#calendar").val(arr.join("-"));
      }
      pageStatistics.date = $("#calendar").val();
      pageStatistics.getData();

  });


  var pageStatistics = new Vue({
      el: '#pageStatistics',
      data: {
        user_info:{},
        type:"date",//date,month
        showhighchartstype:'line',
        date:'',
        data:{},
        maxDate:'',
        pie_chart:{},
        line_chart:{},
        statisticsBasicInfo:{}
      },
      created: function() {
        this.maxDate = "2019-01,"+this.GetDateStr(0);
      },
      mounted: function() {
            this.calendar();
            this.getData();
      },
     methods: {
      showCondtion:function(type){
        this.type=type;
        window.params_type=type;
        if(type=="date"){
            $("#calendar").val(this.GetDateStr(0));
        }
        $(document).trigger("closeMobileCalendar");
        // this.getData();
      },
      GetDateStr: function(AddDayCount) {
          var dd = new Date();
          dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
          var y = dd.getFullYear();
          var m = dd.getMonth()+1;//获取当前月份的日期
          var d = dd.getDate();
          return y+"-"+m+"-"+d;
      },
      getData:function(){
           var that = this;
           app.get("/apiv2/order/statisticsBasicInfo",{date:this.date,type:this.type,store_id:1},function(res){
              var data = res.data;
              that.statisticsBasicInfo = data;
               api.refreshHeaderLoadDone();
           })
          //  x统计数据
          var url ="/apiv2/order/statisticsByDay";
          if(this.type=="month"){
               url ="/apiv2/order/statisticsByMonth";
          }

          app.get(url,{date:this.date,store_id:1},function(res){
             var data = res.data;
             that.data = data;
             that.changeToHighcharts();
             that.date = data.date;
          })
      },
      changeToHighcharts:function(){
        var HighchartsData = this.data;

        /************饼形图*************/
        var categories =[];
        var data=[];
        for (var i = 0 in HighchartsData.pie_chart) {
          if(HighchartsData.pie_chart[i].order_amount>0){
            categories.push(HighchartsData.pie_chart[i].title);
            data.push([HighchartsData.pie_chart[i].title,HighchartsData.pie_chart[i].order_amount]);
          }
        }
        var pie_options={
            chart: {
               // type: 'pie',
                options3d: {
                    enabled: true,
                    alpha: 20,
                    beta: 0
                }
            },
            title: {
                text: data.date
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            yAxis: {
                title: {
                    text: ''
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: ''
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    depth: 35,
                    dataLabels: {
                        enabled: true,
                        format: '{point.name}'
                    }
                }
            },

            xAxis: {
                categories: categories
            },
           series: [

            {
                type: 'pie',
                name: '金额(元)',
                data: data,
                showInLegend: false,
                dataLabels: {
                    enabled: true
                }
          }]
        };
        this.pie_options = pie_options;
        /************折线形图*************/
        categories =[];
        data=[];
        for (var i = 0 in HighchartsData.bar_chart) {
          categories.push(HighchartsData.bar_chart[i].title);
          data.push(HighchartsData.bar_chart[i].order_amount);
        }
        var line_options={
            title: {
                text: '',
                x: -20 //center
            },
            subtitle: {
                text: '',
                x: -20
            },
            xAxis: {
                categories: categories,

            },
            yAxis: {
                title: {
                    text: ''
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: ''
            },
            series: [{
                name: '金额(元)',
                data: data
            }]
        };
        this.line_options = line_options;
        // 实例化统计
        this.showhighcharts();
      },

      showhighcharts:function(type){
        if(type){
          this.showhighchartstype = type;
        }
        if(this.showhighchartstype=="line"){
          // console.log(this.line_options);
          $('#highcharts').highcharts(this.line_options);
        }else{
            // console.log(this.pie_options);
          $('#highcharts').highcharts(this.pie_options);
        }
      },

      calendar:function(){
        var calendar = new lCalendar();
        calendar.init({
             'trigger': "#calendar",
             'type': this.type
         });
      }


     }
  })
}
